package omg.StoreHandling;

import java.io.File;
import omg.DbHandling.DatabaseInt;
import omg.Error.DBError;
import omg.Error.PhysicalStoreError;
import omg.SongHandling.DataHandling.*;
import omg.UserInterfaces.UserInt;

public class StoreManager {

	private static StoreManager instance;

        /* SINGLETON PATTERN */
	private StoreManager() {}
	public static StoreManager getInstance() {
		if (null == StoreManager.instance)
                    StoreManager.instance = new StoreManager();
                return StoreManager.instance;
	}

	/**
	 * 
	 * @param from
	 * @param to
	 * @return 
	 */
	public void alterStoreLocation(File from, File to) throws PhysicalStoreError {
            System.out.println("Changing store location, from: " +from.getPath()+", to: " + to.getPath());
            try{
                if(!from.exists()) {
                    DatabaseInt.getInstance().setConfigValue("StoreLocation", to.getPath());
                } else if(from.renameTo(to)) {
                    DatabaseInt.getInstance().setConfigValue("StoreLocation", to.getPath());
                } else {
                    throw new PhysicalStoreError("Selected folder is not empty");
                }
            }
            catch (DBError ex) {}

	}

	/**
	 * 
	 * @param old
	 * @param alterStringRepresentation
	 * @return 
	 */
	public void alterStringRepresentation(String old, String alterStringRepresentation) {
		throw new UnsupportedOperationException();
	}

	/**
	 * 
	 * @return 
	 */
	public void toggleAutoPersist() {
		throw new UnsupportedOperationException();
	}

        /**
         * Return the file name associated with a song, following the current
         * valid string representation pattern.
         * @param song
         * @return
         */
        public String getSongFileName(Song song) throws DBError {
            String pattern = DatabaseInt.getInstance().getStringRepresentationPattern();
            pattern = pattern.replaceAll("%artist%", song.getArtist());
            pattern = pattern.replaceAll("%title%", song.getTitle());
            pattern = pattern.replaceAll("%album%", song.getAlbum());
            pattern = pattern.replaceAll("%genre%", UserInt.genres[Integer.parseInt(song.getGenre())]);
            if (null != song.getYear())
                pattern = pattern.replaceAll("%year%", song.getYear());
            return pattern;
        }

	/**
	 * 
	 * @param song
	 * @param old_pattern
	 * @param new_pattern
	 * @return 
	 */
	private void move(Song song, String old_pattern, String new_pattern) {
		throw new UnsupportedOperationException();
	}

}